!/bin/sh

#echo -n "Enter Your Want Percent"

SYS_USAGE_MAX=720

while true

PNAME='tomcat'

TOMCAT_ID=`ps -ef | grep "$PNAME" | grep -v "$0" | grep -v "grep" | grep -v "tail" | awk '{print $2}'`

do
	echo '60s执行一次CPU使用率的测试,当前tomcat的PID = '$TOMCAT_ID
	
	sleep 6 

	time=`date +%F--%T`

	TOMCAT_CPU_USE=`top -b -n 1 -p $TOMCAT_ID | grep "$TOMCAT_ID" | awk '{print $9}'| cut -d '.' -f 1`
	
	#echo $TOMCAT_CPU_USE	
	
	if [ $TOMCAT_CPU_USE -lt $SYS_USAGE_MAX ];
	
	then
		echo '当前时间为：'$time';--CPU整体使用率为：'$TOMCAT_CPU_USE'%,当前CPU状态正常'

	else
		echo '当前时间为：'$time';--CPU整体使用率为：'$TOMCAT_CPU_USE'%,当前CPU状态异常，系统将会自动结束并重启tomcat'
		#获取当前tomcat的进程ID
		#ID=`ps -ef | grep "$PNAME" | grep -v "$0" | grep -v "grep" | awk '{print $2}'`
		echo '当前的tomcat的进行ID为：'$ID'，使用kill命令关闭进程,当前进程都为单个，不做循环,等待一分钟之后启动系统'
		kill -9 $TOMCAT_ID
		sleep 5
		#启动/szcg/apache-tomcat-7.0.69/bin/startup.sh---------------------------------------------------------
		/szcg/apache-tomcat-7.0.69/bin/startup.sh
		#启动之后，获取当前最新的tomcat的PID
		TOMCAT_ID=`ps -ef | grep "$PNAME" | grep -v "$0" | grep -v "grep" | grep -v "tail" | awk '{print $2}'`

	fi
	done

exit 0
